package com.datalayer.utils.builder;

import com.datalayer.utils.OrmProcessing;

import java.lang.annotation.IncompleteAnnotationException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/9/12
 * Time: 2:21 PM
 */
public class ClassBuilder implements Builder {

    Class object;
    Class source;
    Method m;

    public ClassBuilder(Method m, Class object, Class source) {
        this.object = object;
        this.source = source;
        this.m = m;
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getONE2ONE() {
        List<T> result = new LinkedList<T>();
        T cc = (T) m.getReturnType();
        if (cc != null && !cc.equals(source)) {
            result.add(cc);
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getMANY2ONE() throws IncompleteAnnotationException {
        List<T> result = new LinkedList<T>();
        result.addAll((Collection<? extends T>) OrmProcessing.getReferencedCollectionClasses(object, source));
        return result;
    }

}
